package frobagroup.controller.backstage;

import frobagroup.entities.backstage.Comment;
import frobagroup.entities.backstage.ImageText;
import frobagroup.entities.backstage.Product;
import frobagroup.pagination.Pagination;
import frobagroup.services.backstage.StaffProductService;
import frobagroup.utils.Constants;
import frobagroup.utils.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Tinsley on 2018/5/16.
 */
@RequestMapping("/webAPI/product")
@RestController
public class StaffProductController {

    @Autowired
    private StaffProductService staffProductService;


    @RequestMapping(value = "/addOrEditProduct", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> addOrEditProduct(
                                                                  @RequestBody Product product, HttpServletRequest request) throws Exception{
        return ResponseModel.success(staffProductService.addOrEditProduct(product,
                (String)request.getAttribute("staffId")),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/addOrEditProductPic", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> addOrEditProductPic(@RequestParam(value = "pic", required = false) MultipartFile pic,
                                                                     @RequestParam(value = "id", required = false) String id,
                                                                     HttpServletRequest request) throws Exception{
        return ResponseModel.success(staffProductService.addOrEditProductPic(pic,id,
                (String)request.getAttribute("staffId")),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/editImageText", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> editImageText(@RequestParam(value = "pic", required = false) MultipartFile pic,
                                                               HttpServletRequest request,@RequestParam(value = "type", required = false) String type,
                                                               @RequestParam(value = "content", required = false) String content,
                                                               @RequestParam(value = "id", required = false) String id,
                                                               @RequestParam(value = "order", required = false) String order,
                                                               @RequestParam(value = "itId", required = false) String itId
    ) throws Exception{
        return ResponseModel.success(staffProductService.editImageText(pic,(String)request.getAttribute("staffId"),content,id,order,type,itId),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/removeImageText", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> removeImageText(HttpServletRequest request,
                                                                 @RequestBody Map<String,String> params
    ) throws Exception{
        staffProductService.removeImageText(params.get("pic"),(String)request.getAttribute("staffId"),params.get("id"),params.get("itId"));
        return ResponseModel.success(null,
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/removeImageTextPic", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> removeImageTextPic(@RequestParam(value = "id", required = false) String id,
                                                                    HttpServletRequest request

    ) throws Exception {
        return ResponseModel.success(staffProductService.removeImageTextPic(id,(String) request.getAttribute("staffId")), Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/findProducts", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> findProducts(@RequestBody Pagination pagination) throws Exception{
        return ResponseModel.success(staffProductService.findProducts(pagination),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/findProductDetial/{id}", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> findProductDetial(@PathVariable String id,HttpServletRequest request) throws Exception{
        return ResponseModel.success(staffProductService.findProductDetial(id,request),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/comment/{pId}", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> comment(@PathVariable String pId, HttpServletRequest request,
                                                         @RequestBody Comment comment
    ) throws Exception{
        return ResponseModel.success(staffProductService.comment(pId,comment,(String)request.getAttribute("id")),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/adjustImageTextOrder/{pId}", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> adjustImageTextOrder(@RequestBody Map<String,List<ImageText>> its,
                                                                    @PathVariable String pId
    ) throws Exception{
        return ResponseModel.success(staffProductService.adjustImageTextOrder(its,pId),
                Constants.RESPONSE_STATUS_SUCESS);
    }

    @RequestMapping(value = "/addOrEditRelatedProduct", method = RequestMethod.POST )
    public ResponseEntity<ResponseModel<Object>> addOrEditRelatedProduct(@RequestBody Product product
    ) throws Exception{
        return ResponseModel.success(staffProductService.addOrEditRelatedProduct(product),
                Constants.RESPONSE_STATUS_SUCESS);
    }



}
